package midas.mw.crt;

import java.util.Enumeration;

//import midas.mw.crt.common.IPMessage;
//import midas.mw.crt.jxta.JxtaModule;
import midas.mw.cfg.CfgLogging;
import midas.mw.cfg.log.Logger;
import net.jxta.discovery.DiscoveryEvent;
import net.jxta.discovery.DiscoveryListener;
import net.jxta.protocol.DiscoveryResponseMsg;
import net.jxta.protocol.PeerAdvertisement;

public class SearchThread extends Thread implements DiscoveryListener
{
	/**
	 * Time to wait for a search
	 */
	private long waitingTime 			= 60000;
	
	/**
	 * MIDAS node identifier
	 */
	private String nodeId			= null;	
	
	/**
	 * String used to provide locks
	 */
	private String responseLock 	= new String("responseLock");
	
	/**
	 * Shows if the node has been found or not
	 */
	private boolean bResolved		= false;
	
	/**
	 * Message Dispatcher instance
	 */
	private MessageDispatcher md	= null;
	
	/**
	 * Constructor
	 * @param md Message Dispatcher instance
	 * @param nodeId MIDAS node identifier
	 */
	public SearchThread (MessageDispatcher md, String nodeId)
	{
		CfgLogging.logEvent(CfgLogging.LOG_MCALL, Logger.COMPONENT_CRT, CfgLogging.SEV_DEBUG, "SearchThread(MessageDispatcher, " + nodeId + ")");
		
		this.md			= md;
		this.nodeId 	= nodeId;
		
		md.getNodeList().setStatus(nodeId, true);
	}
	
	/**
	 * Overriding the run method of class Thread
	 */
	public void run()
	{
		CfgLogging.logEvent(CfgLogging.LOG_MCALL, Logger.COMPONENT_CRT, CfgLogging.SEV_DEBUG, "SearchThread.run()");
		
		synchronized (responseLock) 
		{
			while (!bResolved) 
			{
				try {
					responseLock.wait(waitingTime);
					if (bResolved)
					{						
                        md.getNodeList().setStatus(nodeId, false);
						md.startSendingMessages(nodeId);						
					}
					else				
						md.getNodeList().setStatus(nodeId, false);
				
					md.getSearchingThreads().remove(nodeId);
					return;
						
				} catch (InterruptedException e) {}
			}
		}
	}
	
	/**
	 * Implementing the DiscoveryListener interface
	 */
	public void discoveryEvent(DiscoveryEvent ev) 
	{
		CfgLogging.logEvent(CfgLogging.LOG_MCALL, Logger.COMPONENT_CRT, CfgLogging.SEV_DEBUG, "SearchThread.discoveryEvent(DiscoveryEvent)");
		
		DiscoveryResponseMsg res = ev.getResponse();
		// Get the responding peer's advertisement
		//PeerAdvertisement peerAdv = res.getPeerAdvertisement();
		
		//printout each discovered peer
		PeerAdvertisement adv 	= null;
		Enumeration en 			= res.getAdvertisements();
		if (en != null ) 
		{
			if (en != null ) 
			{
				while (en.hasMoreElements()) 
				{
					adv = (PeerAdvertisement) en.nextElement();
					
					if (adv.getName().equals(nodeId))
					{
						bResolved = true;													
						synchronized(responseLock) {
							responseLock.notify();
						}
						break;
					}
				}
			}
		}   
	}
	
	/**
	 * Stops the search
	 */
	public void stopThread()
	{
		CfgLogging.logEvent(CfgLogging.LOG_MCALL, Logger.COMPONENT_CRT, CfgLogging.SEV_DEBUG, "SearchThread.stopThread()");
		//md.getSearchingThreads().remove(nodeId);
		synchronized(responseLock) {
			responseLock.notify();
		}
	}
}
